package org.dfzt.modules.online.cgform.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dfzt.common.util.oConvertUtils;
import org.dfzt.modules.online.cgform.entity.OnlCgformButton;
import org.dfzt.modules.online.cgform.entity.OnlCgformEnhanceJs;
import org.dfzt.modules.online.cgform.entity.OnlCgformField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




public class EnhanceJsUtil
{
  private static final Logger a = LoggerFactory.getLogger(EnhanceJsUtil.class);

  
  private static final String b = "beforeAdd,beforeEdit,afterAdd,afterEdit,beforeDelete,afterDelete,mounted,created,show";

  
  private static final String c = "\\}\\s*\r*\n*\\s*";

  
  private static final String d = ",";


  
  public static void main(String[] args) {
    String str1 = "aa(row){console.log(112);}\nbb(row){console.log(row);}";
    
    String str2 = "bb";
    System.out.println(a(str1, str2));

    
    ArrayList arrayList = new ArrayList();
    OnlCgformButton onlCgformButton1 = new OnlCgformButton();
    onlCgformButton1.setButtonCode("bb");
    onlCgformButton1.setButtonStyle("link");
    arrayList.add(onlCgformButton1);
    
    OnlCgformButton onlCgformButton2 = new OnlCgformButton();
    onlCgformButton2.setButtonCode("aa");
    onlCgformButton2.setButtonStyle("link");
    arrayList.add(onlCgformButton2);
    
    System.out.println(b(str1, arrayList));
  }











  
  public static String a(String paramString1, String paramString2) {
    String str1 = "(" + paramString2 + "\\s*\\(row\\)\\s*\\{)";
    String str2 = paramString2 + ":function(that,row){const getAction=this._getAction,postAction=this._postAction,deleteAction=this._deleteAction;";
    String str3 = b(paramString1, "\\}\\s*\r*\n*\\s*" + str1, "}," + str2);
    if (str3 == null) {
      paramString1 = c(paramString1, str1, str2);
    } else {
      paramString1 = str3;
    } 
    return a(paramString1, paramString2, null);
  }








  
  public static String a(String paramString1, String paramString2, String paramString3) {
    String str1 = "(" + oConvertUtils.getString(paramString3) + paramString2 + "\\s*\\(\\)\\s*\\{)";
    String str2 = paramString2 + ":function(that){const getAction=this._getAction,postAction=this._postAction,deleteAction=this._deleteAction;";
    String str3 = b(paramString1, "\\}\\s*\r*\n*\\s*" + str1, "}," + str2);
    if (str3 == null) {
      paramString1 = c(paramString1, str1, str2);
    } else {
      paramString1 = str3;
    } 
    return paramString1;
  }







  
  public static String b(String paramString1, String paramString2, String paramString3) {
    Pattern pattern = Pattern.compile(paramString2);
    Matcher matcher = pattern.matcher(paramString1);
    if (matcher.find()) {
      return paramString1.replace(matcher.group(0), paramString3);
    }
    
    return null;
  }

  
  public static String c(String paramString1, String paramString2, String paramString3) {
    String str = b(paramString1, paramString2, paramString3);
    if (str != null) {
      return str;
    }
    return paramString1;
  }


  
  public static String a(String paramString, List<OnlCgformButton> paramList) {
    a.info("最终的增强JS", paramString);
    return "class OnlineEnhanceJs{constructor(getAction,postAction,deleteAction){this._getAction=getAction;this._postAction=postAction;this._deleteAction=deleteAction;}" + paramString + "}";
  }







  
  public static String b(String paramString1, String paramString2) {
    String str1 = "(\\s+" + paramString2 + "\\s*\\(\\)\\s*\\{)";
    String str2 = paramString2 + ":function(that,event){";
    String str3 = b(paramString1, "\\}\\s*\r*\n*\\s*" + str1, "}," + str2);
    if (str3 == null) {
      paramString1 = c(paramString1, str1, str2);
    } else {
      paramString1 = str3;
    } 
    return paramString1;
  }
  
  public static String a(String paramString) {
    String str = "function OnlineEnhanceJs(getAction,postAction,deleteAction){return {_getAction:getAction,_postAction:postAction,_deleteAction:deleteAction," + paramString + "}}";
    a.info("最终的增强JS", str);
    return str;
  }






  
  public static String b(String paramString, List<OnlCgformButton> paramList) {
    paramString = c(paramString, paramList);
    String str = "function OnlineEnhanceJs(getAction,postAction,deleteAction){return {_getAction:getAction,_postAction:postAction,_deleteAction:deleteAction," + paramString + "}}";
    a.info("最终的增强JS", str);
    return str;
  }
  
  public static String c(String paramString, List<OnlCgformButton> paramList) {
    if (paramList != null) {
      for (OnlCgformButton onlCgformButton : paramList) {
        String str = onlCgformButton.getButtonCode();
        if ("link".equals(onlCgformButton.getButtonStyle())) {
          paramString = a(paramString, str); continue;
        }  if ("button".equals(onlCgformButton.getButtonStyle()) || "form".equals(onlCgformButton.getButtonStyle())) {
          paramString = a(paramString, str, null);
        }
      } 
    }
    for (String str : "beforeAdd,beforeEdit,afterAdd,afterEdit,beforeDelete,afterDelete,mounted,created,show".split(",")) {
      if ("beforeAdd,afterAdd,mounted,created,show".indexOf(str) >= 0) {
        paramString = a(paramString, str, null);
      } else {
        paramString = a(paramString, str);
      } 
    } 
    return paramString;
  }




  
  public static void a(OnlCgformEnhanceJs paramOnlCgformEnhanceJs, String paramString, List<OnlCgformField> paramList) {
    if (paramOnlCgformEnhanceJs == null || oConvertUtils.isEmpty(paramOnlCgformEnhanceJs.getCgJs())) {
      return;
    }
    
    String str = " " + paramOnlCgformEnhanceJs.getCgJs();
    a.info("one enhanceJs begin==> " + str);
    
    Pattern pattern = Pattern.compile("(\\s{1}onlChange\\s*\\(\\)\\s*\\{)");
    Matcher matcher = pattern.matcher(str);
    if (matcher.find()) {
      a.info("---JS 增强转换-main--enhanceJsFunctionName----onlChange");
      str = a(str, "onlChange", "\\s{1}");
      for (OnlCgformField onlCgformField : paramList)
      {
        str = b(str, onlCgformField.getDbFieldName());
      }
    } 
    a.info("one enhanceJs end==> " + str);
    paramOnlCgformEnhanceJs.setCgJs(str);
  }




  
  public static void b(OnlCgformEnhanceJs paramOnlCgformEnhanceJs, String paramString, List<OnlCgformField> paramList) {
    if (paramOnlCgformEnhanceJs == null || oConvertUtils.isEmpty(paramOnlCgformEnhanceJs.getCgJs())) {
      return;
    }
    a.info(" sub enhanceJs begin==> " + paramOnlCgformEnhanceJs);
    String str1 = paramOnlCgformEnhanceJs.getCgJs();
    
    String str2 = paramString + "_" + "onlChange";


    
    Pattern pattern = Pattern.compile("(" + str2 + "\\s*\\(\\)\\s*\\{)");
    
    Matcher matcher = pattern.matcher(str1);
    if (matcher.find()) {
      a.info("---JS 增强转换-sub-- enhanceJsFunctionName----" + str2);
      str1 = a(str1, str2, null);
      for (OnlCgformField onlCgformField : paramList)
      {
        str1 = b(str1, onlCgformField.getDbFieldName());
      }
    } 
    a.info(" sub enhanceJs end==> " + str1);
    paramOnlCgformEnhanceJs.setCgJs(str1);
  }
}
